/*
 * JPasswordChangeDialog.java
 *
 * Created on 20 kwiecień 2008, 16:53
 */

package schoolmanagement.dialogs;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import org.jdesktop.application.Action;
import schoolmanagement.controller.*;

/**
 *
 * @author  deely
 */
public class JPasswordChangeDialog extends javax.swing.JDialog {
    
    /** Creates new form JPasswordChangeDialog */
    public JPasswordChangeDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }
    
   
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        m_jpassfieldCurrentPassword = new javax.swing.JPasswordField();
        m_jpassfieldNewPassword = new javax.swing.JPasswordField();
        m_jpassfieldPasswordConfirm = new javax.swing.JPasswordField();
        m_jbtnApply = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setName("Form"); // NOI18N
        setResizable(false);

        jLabel1.setText("Aktualne haslo"); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jLabel2.setText("Nowe haslo"); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        jLabel3.setText("Powtorz haslo"); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(schoolmanagement.SchoolmanagementApp.class).getContext().getResourceMap(JPasswordChangeDialog.class);
        m_jpassfieldCurrentPassword.setText(resourceMap.getString("m_jpassfieldCurrentPassword.text")); // NOI18N
        m_jpassfieldCurrentPassword.setName("m_jpassfieldCurrentPassword"); // NOI18N

        m_jpassfieldNewPassword.setText(resourceMap.getString("m_jpassfieldNewPassword.text")); // NOI18N
        m_jpassfieldNewPassword.setName("m_jpassfieldNewPassword"); // NOI18N

        m_jpassfieldPasswordConfirm.setText(resourceMap.getString("m_jpassfieldPasswordConfirm.text")); // NOI18N
        m_jpassfieldPasswordConfirm.setName("m_jpassfieldPasswordConfirm"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(schoolmanagement.SchoolmanagementApp.class).getContext().getActionMap(JPasswordChangeDialog.class, this);
        m_jbtnApply.setAction(actionMap.get("ChangePassword_Click")); // NOI18N
        m_jbtnApply.setText("Zapisz zmiany"); // NOI18N
        m_jbtnApply.setName("m_jbtnApply"); // NOI18N

        jButton2.setAction(actionMap.get("CancelBtn_Click")); // NOI18N
        jButton2.setText("Anuluj"); // NOI18N
        jButton2.setName("jButton2"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(m_jpassfieldNewPassword)
                            .addComponent(m_jpassfieldCurrentPassword)
                            .addComponent(m_jpassfieldPasswordConfirm, javax.swing.GroupLayout.PREFERRED_SIZE, 154, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(31, 31, 31)
                        .addComponent(m_jbtnApply, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGap(18, 18, 18)
                        .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(m_jpassfieldCurrentPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(m_jpassfieldNewPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(m_jpassfieldPasswordConfirm, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton2)
                    .addComponent(m_jbtnApply, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                JPasswordChangeDialog dialog = new JPasswordChangeDialog(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    private boolean m_bResult = false;
    public boolean getResult()
    {
        return m_bResult;
    }
    
    public String getNewPassword()
    {
        return String.valueOf(m_jpassfieldNewPassword.getPassword());
    }

    @Action
    public void ChangePassword_Click() {
        String strNewPass = String.valueOf(m_jpassfieldNewPassword.getPassword());
        String strNewPassRetype = String.valueOf(m_jpassfieldPasswordConfirm.getPassword());
        if(strNewPass.equals(strNewPassRetype))
        {
            m_bResult = true;
            if( User.ChangePassword( String.valueOf(m_jpassfieldCurrentPassword.getPassword()), strNewPassRetype ) ) {
                this.setVisible(false);
            } else {
                JOptionPane.showMessageDialog(null, "Błąd zmiany hasła","Błąd",JOptionPane.ERROR_MESSAGE);
            }
        }
        else
        {
            JOptionPane.showMessageDialog(null, "Hasła nie zgadzają się","Błąd",JOptionPane.ERROR_MESSAGE);
        }
        
    }

    @Action
    public void CancelBtn_Click() {
        m_bResult = false;
        this.setVisible(false);
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JButton m_jbtnApply;
    private javax.swing.JPasswordField m_jpassfieldCurrentPassword;
    private javax.swing.JPasswordField m_jpassfieldNewPassword;
    private javax.swing.JPasswordField m_jpassfieldPasswordConfirm;
    // End of variables declaration//GEN-END:variables
    
}
